/*****************************************************************************
 * ui_sys_explain.c: Copyright 2023 (c) CoreLink
 * Desc:
 *
 * Author: Flying (weekend@111.com)
 * Date: 2023-09-07 14:22:15
 * Notes:
 *
 * ---------------------------------------------------------------------------
 * LastEditors:
 * LastEditTime:
 *****************************************************************************/
/*------------------------------- Includes ----------------------------------*/
#include "ui_sys_explain.h"
#include "ui_common.h"
/*------------------- 全局定义和声明 -------------------*/

/*---------------------- 常量/宏定义 -----------------------*/

/*----------------------- 类型声明 ---------------------------------*/

/*----------------------- 变量声明 -----------------------------*/

/*----------------------- 函数原型 --------------------------------*/
static void ui_sys_explain_open_changer_anim_cb(void *var, int32_t v);

/*----------------------- 功能实现 --------------------------------*/

ui_sys_explain_info_t *ui_sys_explain_create(lv_obj_t *parent)
{
    ui_sys_explain_info_t *info = (ui_sys_explain_info_t *)MY_MALLOC(sizeof(ui_sys_explain_info_t));
    memset(info, 0, sizeof(ui_sys_explain_info_t));
    info->parent = parent;
    return info;
}

/**
 * @description:
 * @param {*}
 * @return {*}
 */
void ui_sys_explain_body_create(ui_sys_explain_info_t *info)
{
    LV_IMG_DECLARE(explain1);
    LV_IMG_DECLARE(explain2);
    LV_IMG_DECLARE(explain3);
    LV_IMG_DECLARE(explain4);
    const lv_img_dsc_t *dscs[] = {&explain1, &explain2, &explain3, &explain4};
    for (int index = 1; index <= 4; index++)
    {
        lv_obj_t *tile = lv_tileview_add_tile(info->cont, 0, index - 1, LV_DIR_VER);

        lv_obj_t *label = lv_label_create(tile);
        lv_obj_set_style_text_font(label, ui_font_get_font(40 * MY_UI_H_ZOOM), 0);
        lv_label_set_text_fmt(label, "说明页：%d", index);
        lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 10 * MY_UI_H_ZOOM);

        lv_obj_t *img = lv_img_create(tile);
        lv_img_set_src(img, dscs[index - 1]);
        lv_obj_set_size(img, 300, 180);
        lv_obj_center(img);
    }
}

int ui_sys_explain_show(ui_sys_explain_info_t *info)
{
    if (info->cont)
    {
        return -1;
    }

    info->cont = lv_tileview_create(info->parent);
    lv_obj_set_style_radius(info->cont, 0, 0);

    info->cont->user_data = (void *)info;
    ui_sys_explain_body_create(info);

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, info->cont);
    lv_anim_set_exec_cb(&a, ui_sys_explain_open_changer_anim_cb);
    lv_anim_set_values(&a, 0, 256);
    lv_anim_set_time(&a, 200);
    lv_anim_start(&a);
    return 0;
}

int ui_sys_explain_delete(ui_sys_explain_info_t *info)
{
    // TODO
    return -1;
}

/**
 * @description: 动画轨迹
 * @param {void} *var
 * @param {int32_t} v
 * @return {*}
 */
static void ui_sys_explain_open_changer_anim_cb(void *var, int32_t v)
{
    lv_obj_t *obj = (lv_obj_t *)var;
    lv_coord_t w, h;

    w = lv_map(v, 0, 256, 10, MY_UI_W_MAX * 0.95);
    h = lv_map(v, 0, 256, 10, MY_UI_H_MAX * 0.95);
    lv_obj_set_size(obj, w, h);
    if (v > 250)
    {
        lv_obj_align(obj, LV_ALIGN_CENTER, 0, 0);
    }
    else
    {
        lv_obj_align(obj, LV_ALIGN_BOTTOM_MID, 0, 0);
    }
    lv_opa_t o;
    o = lv_map(v, 0, 256, 0, LV_OPA_100);
    lv_obj_set_style_bg_opa(obj, o, 0);
}

int ui_sys_explain_exit(ui_sys_explain_info_t *info)
{
    if (info->cont)
    {
        lv_obj_del(info->cont);
        info->cont = NULL;
        return true;
    }
    return false;
}

/*---------------------------------------------------------------------------*/